import { Response } from 'express'
import { ApiResponse } from '../types'

export const successResponse = <T = any>(
  res: Response,
  data?: T,
  message?: string,
  statusCode: number = 200
): void => {
  const response: ApiResponse<T> = {
    success: true,
    data,
    message,
  }
  
  res.status(statusCode).json(response)
}

export const errorResponse = (
  res: Response,
  error: string,
  statusCode: number = 500
): void => {
  const response: ApiResponse = {
    success: false,
    error,
  }
  
  res.status(statusCode).json(response)
}

export const createdResponse = <T = any>(
  res: Response,
  data: T,
  message: string = 'Resource created successfully'
): void => {
  successResponse(res, data, message, 201)
}

export const noContentResponse = (
  res: Response,
  message: string = 'No content'
): void => {
  successResponse(res, undefined, message, 204)
}

export const badRequestResponse = (
  res: Response,
  error: string = 'Bad request'
): void => {
  errorResponse(res, error, 400)
}

export const unauthorizedResponse = (
  res: Response,
  error: string = 'Unauthorized'
): void => {
  errorResponse(res, error, 401)
}

export const forbiddenResponse = (
  res: Response,
  error: string = 'Forbidden'
): void => {
  errorResponse(res, error, 403)
}

export const notFoundResponse = (
  res: Response,
  error: string = 'Resource not found'
): void => {
  errorResponse(res, error, 404)
}

export const conflictResponse = (
  res: Response,
  error: string = 'Conflict'
): void => {
  errorResponse(res, error, 409)
}

export const unprocessableEntityResponse = (
  res: Response,
  error: string = 'Unprocessable entity'
): void => {
  errorResponse(res, error, 422)
}

export const tooManyRequestsResponse = (
  res: Response,
  error: string = 'Too many requests'
): void => {
  errorResponse(res, error, 429)
}

export const internalServerErrorResponse = (
  res: Response,
  error: string = 'Internal server error'
): void => {
  errorResponse(res, error, 500)
}

export const serviceUnavailableResponse = (
  res: Response,
  error: string = 'Service unavailable'
): void => {
  errorResponse(res, error, 503)
}